<template>
	<view class="h100v">
		<goods-menu  ref="menu" :tableId="tableId" :category-list="categoryList"  @add='add' @dec='dec' />
		<shopping-cart ref="cart" :cart-data="cartData" :cart-list="cartList" :tableId="tableId" @clearCart="clearCart" @add='add' @dec='dec' />
	</view>
</template>

<script>
	import GoodsMenu from "./components/GoodsMenu"
	import ShoppingCart from "./components/ShoppingCart"
	export default {
		components:{
			GoodsMenu,
			ShoppingCart
		},
		data(){
			return {
				categoryList:[{goods:[]}],
				cartList:[],
				cartData:{},
				tableId:0,
				show:false
			}
		},
		onLoad(option) {
			var name = option.tableName || '快速点单'
			this.util.setNT(`协助点单-${name}`);
			this.tableId = option.tableId || 0
			uni.$on('clearCart',this.clearCart)
		},
		onShow() {
			this.init()
		},
		methods:{
			async init() {
				let {data} = await this.util.request({
					'url': this.api.goodsInfo,
				})
				// this.goodsList = utils.deepCopy(this.sjxx.recommendData)
				this.categoryList = data
				this.handleUpdata('init')
			},
			async handleUpdata(flag){
				var tableId = this.tableId
				let res = await this.util.request({
					'url': this.api.getCarList,
					data:{
						tableId:tableId
					}
				})
				var list = res.cashierData || res.data || []
				this.cartData = {
					carCount:res.carCount || res.count || 0,
					carMoney:res.carMoney || res.money || ''
				}
				this.cartList = list
				// 初始化购物车记录  初始化购物车和清空购物车时候请求
				if(flag === 'init'){
					this.categoryList.map((category,c)=>{
						category.goods.map((goods,g)=>{
							list.map(item=>{
								if(item.goodsId === goods.id){
									this.categoryList[c].goods[g].num = item.num
								}
							})
						})
					})
				}else if(flag === 'clearCart'){
					this.categoryList.map((category,c)=>{
						category.goods.map((goods,g)=>{
									this.categoryList[c].goods[g].num = 0
						})
					})
					console.log(this.categoryList)
				}
			},
			clearCart(tableId){
				this.util.request({
					'url': this.api.delCar,
					method: 'POST',
					data: {
						tableId: tableId || this.tableId
					}
				})
				this.handleUpdata('clearCart')
			},
			async dec(e) {
					this.showjz = true
					this.updList({
						type: 2,
						e,
					})
			},
			async add(e) {
					if (e.g.SalesStock && e.g.SalesStock <= 0) return this.util.message('商品已售罄', 3)
					if (e.g.stock <= 0) return this.util.message('商品已售罄', 3)
					this.showjz = true
					if ((e.g.isSpecs != 1 && e.g.isMaterial != 1 && e.g.isAttr != 1) || e.g.hasOwnProperty('groupId')) {
						this.updList({
							type: 1,
							e,
						})
					} else {
						this.$refs.menu.showGg = true
						this.$refs.menu.goodsInfo = e.g
						this.showjz = false
					}
			},
			
			async supdCar(params) {
				let res = await this.util.request({
					'url': this.api.saveShopCar,
					ct: 1,
					method: 'POST',
					data: params
				})
				return res.count
			},
			async updList(p) {
				try {
					let count = await this.supdCar(p.e.addwz == '1' ? p.e.g.ggnum ? { //选规格加
						type: p.type,
						goodsId: p.e.g.id,
						groupId: p.e.g.groupId || '',
						material: p.e.g.material || [],
						attribute: p.e.g.attribute || '',
						num: p.e.g.ggnum,
						tableId:this.tableId
					} : { //列表加
						type: p.type,
						goodsId: p.e.g.id,
						tableId:this.tableId
					} : { 
						type: p.type,
						goodsId: p.e.g.goodsId,
						id: p.e.g.id,
						tableId:this.tableId
					})
					if (count >= 0) {
						//改变商品列表里的数量
						this.categoryList.map((category,c)=>{
							category.goods.map((goods,g)=>{
									if(p.e.addwz == '1'  && p.e.g.id === goods.id){
										this.categoryList[c].goods[g].num = count
									}else if(p.e.addwz == '2'  && p.e.g.id === goods.id){
										this.categoryList[c].goods[g].num = count
									}
							})
						})
					}
					this.handleUpdata()
					this.showjz = false
				} catch (e) {
					this.showjz = false
					if (e.code === 2) {
						this.util.message(e.msg, 3)
						return
					}
					console.log(e)
				}
			}
		}
	}
</script>

<style>
</style>
